package panel.eating
{
	import assists.ButtonUtils;
	import assists.HtmlText;
	import assists.Lang;
	import assists.TextFieldUtils;
	import assists.URI;
	import assists.log.Log;
	import assists.resource.ImageResource;
	import assists.resource.ResourceLoader;
	
	import flash.display.Bitmap;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TouchEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	
	import panel.stars.StarGroup;
	import panel.viewSpots.navigation.ScrollTxtHor;
	
	import struct.TourElementBase;
	
	public class EatingRestaurantBox extends Sprite
	{
		private var _loader:ResourceLoader;
		private var _lang:Lang;
		private var _uri:URI;
		
		private var _width:Number;
		private var _height:Number;
		
		private var back:Sprite;
		private var iconContainer:Sprite;
		
		private var titleText:ScrollTxtHor;
		private var descText:TextField;
		private var distanceText:TextField;
		
//		private var mapGuideBtn:ButtonUtils;
		private var commentNumText:TextField;
		
		private var shopLocation:Array;
		
		private var starLevel:StarGroup;
		
		private var _onMapGuide:Function;
		private var _onShowDetail:Function;
		
		private var shopInfo:Object;
		private var img:ImageResource;
		
		public var outStarNum:int = 0;
		public var outDistance:Number = 0;
		public var outCosts:Number = 0;
		
		private var currentShopId:int = 0;
		
		private var startPoint:Point;
		//		private var touchId:int = 0;
		
		private var _onClearRecord:Function;
		
		private var smallBack:Bitmap;
		private var btnBack:Bitmap;
		
		public function EatingRestaurantBox(loader:ResourceLoader,lang:Lang,wid:Number,hei:Number,uri:URI)
		{
			_loader = loader;
			_lang = lang;
			_uri = uri;
			
			_width = wid;
			_height = hei;
			
			shopLocation = [-1000,-1000];
			
			init();
		}
		
		private function init():void
		{
			back= new Sprite;
			
			this.addChild(back);
			
			smallBack = new _loader.uiManager.componentBack as Bitmap;
			smallBack.width = _width;
			smallBack.height = _height;
			back.addChild(smallBack);
			
			iconContainer = new Sprite;
			iconContainer.graphics.lineStyle(1,0x000000,.5);
			iconContainer.graphics.beginFill(0x555555,.5);
			iconContainer.graphics.drawRoundRect(0,0,_height,_height,5);
			iconContainer.graphics.endFill();
			back.addChild(iconContainer);	
			
			titleText = new ScrollTxtHor();
			titleText.showWid = _width * .4;
			titleText.x = _height + 2;
			back.addChild(titleText);
			
			descText = new TextField;
			descText.mouseEnabled = false;
			descText.multiline = true;
			descText.wordWrap = true;
			back.addChild(descText);
			descText.x = _height + 2;
			descText.y = _height / 2;
			
			distanceText = new TextField;
			distanceText.mouseEnabled = false;
			distanceText.multiline = false;
			distanceText.wordWrap = false;
			back.addChild(distanceText);
			distanceText.x = _width - 2;
			distanceText.y = _height / 2;
			
			starLevel = new StarGroup(StarGroup.READ_STARS);
			back.addChild(starLevel);
			starLevel.scaleX = .6;
			starLevel.scaleY = .6;
			
//			btnBack = new _loader.uiManager.btnBack as Bitmap;
//			btnBack.width = _width * .25;
//			btnBack.height = _height * .3;
			
//			var textInfo:String = _lang.client("查看位置按钮文字");
//			mapGuideBtn = new ButtonUtils(btnBack,showGuide,HtmlText.formatSizeColor(textInfo,0xFFFF00));
//			this.addChild(mapGuideBtn);
//			mapGuideBtn.x = _width - mapGuideBtn.width - 5;
//			mapGuideBtn.y = _height * 1 / 2;
			
			back.addEventListener(MouseEvent.MOUSE_DOWN,touchBeginHandler);//TouchEvent.TOUCH_BEGIN
			back.addEventListener(MouseEvent.MOUSE_UP,showDetailHandler);//TouchEvent.TOUCH_END
			
			commentNumText = new TextField;
			commentNumText.mouseEnabled = false;
			commentNumText.multiline = false;
			commentNumText.wordWrap = false;
			back.addChild(commentNumText);
			
			commentNumText.y = _height * .1;
		}
		
		protected function touchBeginHandler(e:MouseEvent):void
		{
			//			touchId = e.touchPointID;
			if(startPoint == null){
				startPoint = new Point(e.stageX,e.stageY);
			}else{
				startPoint.x = e.stageX;
				startPoint.y = e.stageY;
			}
			
			localToGlobal(startPoint);
		}
		
		protected function showDetailHandler(e:MouseEvent):void
		{
			var loc:Point = new Point(e.stageX,e.stageY);
			localToGlobal(loc);
			
			if(checkDistance(loc,startPoint)){
				
				e.stopImmediatePropagation();
				if(_onShowDetail is Function){
					_onShowDetail(shopInfo);
				}
			}
			
			if(_onClearRecord is Function){
				_onClearRecord();
			}
		}
		
		public function clearStart():void
		{
			startPoint = null;
		}
		
		private function checkDistance(p1:Point,p2:Point):Boolean
		{
			if(p2 != null){
				var dis:Number = 0;
				dis = Math.sqrt(Math.abs(Math.pow(p1.x - p2.x,2) + Math.pow(p1.y - p2.y,2)));
				var bool:Boolean;
				
				if(dis > _height / 2){
					return false;
				}
				return true;
			}
			return false;
		}
		
		private function showGuide():void
		{
			if(_onMapGuide is Function){
				_onMapGuide(shopInfo);
			}
		}
		
		public function set currentLoaction(list:Array):void
		{
			if(shopLocation != [-1000,-1000]){
				var distance:int = int(Math.sqrt(Math.pow((shopLocation[0] - list[0]),2) - Math.pow((shopLocation[1] - list[1]),2)));
				//				TextFieldUtils.setHtmlText(distanceText,HtmlText.formatSizeColor(distance + "米",0x446699,16));
				//				distanceText.width = distanceText.textWidth + 5;
				//				distanceText.height = distanceText.textHeight + 5;
				//				
				//				distanceText.x = titleText.x + titleText.width;
				//				distanceText.y =  _height / 2 - distanceText.height - 1
				
				outDistance = distance;
			}
		}
		
		public function changeData(obj:Object):void
		{
			if(obj){
				shopInfo = obj;
				
				currentShopId = obj.id;
				
				//				TextFieldUtils.setHtmlText(titleText,"<b>" + HtmlText.formatSizeColor(obj.name,0x000000) + "</b>",true,true);
				//				titleText.width = titleText.textWidth + 5;
				//				titleText.height = titleText.textHeight + 5;
				//				titleText.y = _height / 2 - titleText.height - 1;
				
				titleText.label("<b>" + obj.name + "</b>",0x000000,20);
				titleText.y = _height / 2 - titleText.height - 1;
				
				TextFieldUtils.setHtmlText(descText,"<b>" + HtmlText.formatSizeColor(obj.desc,0x883366,14) + "</b>");
				descText.width = _width * 1 / 2;//descText.textWidth + 5;
				descText.height = descText.textHeight + 5;
				descText.y = _height / 2 + 1;
				
				if(descText.y + descText.height > _height){
					descText.height = _height - descText.y - 5;
				}
				
				clearImage();
				img = new ImageResource(_loader,obj.url,repositionImg);
				iconContainer.addChild(img);
				repositionImg();
				
				starLevel.starLevel = obj.starLevel;
				starLevel.y = _height / 2 - starLevel.height / 2;
				starLevel.x = _width - starLevel.width - 5;
				
				function repositionImg():void
				{
					img.width = _height;
					img.height = _height;
					img.x = (_height - img.width) / 2;
					img.y = (_height - img.height) / 2;
				}
				
				shopLocation = obj.location;
				
				outStarNum = obj.starLevel;
				outCosts = 0;
				
				//				TextFieldUtils.setHtmlText(commentNumText,HtmlText.formatSizeColor(_lang.client("评价人数",{num:obj.commentPeopleNum}),0xFF0000,18));
				//				commentNumText.width = commentNumText.textWidth + 5;
				//				commentNumText.height = commentNumText.textHeight + 4;
				//				
				//				commentNumText.x = (starLevel.width - commentNumText.width) / 2 + starLevel.x;
			}
		}
		
		private function clearImage():void
		{
			if(img != null){
				iconContainer.removeChild(img);
				img = null;
			}
		}
		
		public function set mapGuide(func:Function):void
		{
			_onMapGuide = func;
		}
		
		public function set showDetail(func:Function):void
		{
			_onShowDetail = func;
		}
		
		public function set clearRecord(func:Function):void
		{
			_onClearRecord = func;
		}
		
		public override function get height():Number
		{
			return _height;
		}
		
		public override function get width():Number
		{
			return _width;
		}
		
		public function clearThis():void
		{
			iconContainer = null;
			titleText.clear();
			titleText = null;
			descText = null;
			distanceText = null;
			starLevel = null;
			
//			mapGuideBtn.clearThis();
//			mapGuideBtn = null;
			
			back = null;
		}
	}
}